using System;
using System.Reflection;

namespace UnitTesting.Fixturing
{
	public class DefaultActivator : IActivator
	{
		static readonly IActivator _instance = new DefaultActivator();

		public static IActivator Instance
		{
			get { return _instance; }
		}

		DefaultActivator()
		{
		}

		public object Create(ActivatorParams activatorParams)
		{
			Ensure.IsNotNull(() => activatorParams.TargetType);
			Ensure.IsNotNull(() => activatorParams.Arguments);

			try
			{
				return Activator.CreateInstance(
					activatorParams.TargetType,
					BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
					null,
					activatorParams.Arguments,
					null);
			}
			catch (MissingMethodException)
			{
				return NotResolved.Value;
			}
			catch (MemberAccessException)
			{
				return NotResolved.Value;
			}
		}
	}
}